#include "button.h"
#include "main.h"

// 使用PA0进行测试
typedef enum
{
    E_KEY_SINGLE,
    E_KEY_DOUBLE,
    E_KEY_LONG_PRESS,
} EnumKeyAction;

typedef enum
{
    KEY_NONE,
    KEY1,
    KEY2,
    KEY3,
    KEY4,
    KEY5,
} EnumKeyPadTags;

static uint8_t keypad_active_tag = KEY_NONE;

#define KEY_ACTIVE_LEVEL (0) // 按键按下时的电平值
#define READ_GPIO_INPUT_LEVEL_RETURN(gpio_port, pins, _ret)    \
    if (HAL_GPIO_ReadPin(gpio_port, pins) == KEY_ACTIVE_LEVEL) \
    {                                                          \
        return _ret;                                           \
    }

// 将按键动作与按键名进行绑定
static EnumKeyPadTags get_keypad_state(void)
{
    READ_GPIO_INPUT_LEVEL_RETURN(GPIOA, GPIO_PIN_0, KEY1);
    return KEY_NONE;
}

// 获取当前活动的按键名(禁止修改)
EnumKeyPadTags api_get_keypad_active_tag()
{
    uint8_t _ret = KEY_NONE;
    if (keypad_active_tag != KEY_NONE)
    {
        _ret = keypad_active_tag;
        keypad_active_tag = KEY_NONE;
    }
    return _ret;
}

void KEY_SEVER(void)
{
    if (api_get_keypad_active_tag() == KEY1)
    {
        printf("key1\n");
    }
}

void timer_keypad_scan_callback(void)
{
    static uint8_t current_key_state, previous_key_state;
    previous_key_state = current_key_state;
    current_key_state = get_keypad_state();
    if (current_key_state == KEY_NONE && previous_key_state != KEY_NONE)
    {
        keypad_active_tag = previous_key_state;
    }
}
